//创建用户相关的小仓库
import { defineStore } from 'pinia'
//引入接口
import { reqLogin, reqLogout, reqUserInfo } from '@/api/user'
import type {
  loginFormData,
  loginResponseData,
  InfoResponseData,
} from '@/api/user/type'
import type { UserState } from '@/store/modules/types/type'
//引入操作本地存储的工具方法
import { GET_TOKEN, SET_TOKEN, REMOVE_TOKEN } from '@/utils/token'
//引入路由(常量路由)
import { constantRoute } from '@/router/routes'

//创建用户小仓库
let useUserStore = defineStore('user', {
  //小仓库存储数据的地方
  state: (): UserState => {
    return {
      token: GET_TOKEN(),
      menuRoutes: constantRoute,
      username: localStorage.getItem('username') || '',
      avatar: localStorage.getItem('avatar') || '',
      id: Number(localStorage.getItem('id')) || 0,
      nickname: localStorage.getItem('nickname') || '',
      password: localStorage.getItem('password') || '',
      login_time: localStorage.getItem('login_time') || '',
    }
  },
  //异步|逻辑的地方
  actions: {
    async userLogin(data: loginFormData) {
      let result: loginResponseData = await reqLogin(data)
      console.log(result)
      if (result.code === 200) {
        this.token = result.data.token as string
        this.username = result.data.user.username
        this.avatar = result.data.user.avatar
        this.id = result.data.user.id
        this.nickname = result.data.user.nickname
        this.password = result.data.user.password
        this.login_time = result.data.user.login_time

        SET_TOKEN(result.data.token as string)
        localStorage.setItem('username', result.data.user.username)
        localStorage.setItem('avatar', result.data.user.avatar)
        localStorage.setItem('id', String(result.data.user.id))
        localStorage.setItem('nickname', result.data.user.nickname)
        localStorage.setItem('password', result.data.user.password)
        localStorage.setItem('login_time', result.data.user.login_time)

        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    },

    async getUserInfo() {
      let result: InfoResponseData = await reqUserInfo()
      console.log(result)
      if (result.code == 200) {
        this.username = result.data.username
        this.avatar = result.data.avatar
        this.id = result.data.id
        this.nickname = result.data.nickname
        this.password = result.data.password
        this.login_time = result.data.login_time

        localStorage.setItem('username', result.data.username)
        localStorage.setItem('avatar', result.data.avatar)
        localStorage.setItem('id', String(result.data.id))
        localStorage.setItem('nickname', result.data.nickname)
        localStorage.setItem('password', result.data.password)
        localStorage.setItem('login_time', result.data.login_time)

        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
    // 其他 actions
    async userLogout() {
      let result: any = await reqLogout()
      console.log(result)
      if (result.code === 200) {
        this.token = ''
        this.username = ''
        this.avatar = ''
        this.id = 0
        this.nickname = ''
        this.password = ''
        this.login_time = ''

        REMOVE_TOKEN()
        localStorage.removeItem('username')
        localStorage.removeItem('avatar')
        localStorage.removeItem('id')
        localStorage.removeItem('nickname')
        localStorage.removeItem('password')
        localStorage.removeItem('login_time')

        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
  },
  getters: {},
})
//对外暴露获取小仓库方法
export default useUserStore
